package com.iris.live.services.config;


import com.iris.live.services.data.repositories.Lsh经销商表Repository;
import com.iris.live.services.data.repositories.ReportConfigRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by HangLiu on 2017/9/7.
 */
@Component
public class GroovyFormularConfig {
    private static final Logger logger = LoggerFactory.getLogger(GroovyFormularConfig.class);

    private static Lsh经销商表Repository lsh经销商表Repository;

    private static ReportConfigRepository reportConfigRepository;

    private static List<List> units=new ArrayList<>();

    //@PostConstruct
    public void loadFormular() throws Exception {
        System.out.println("被调用了");
        setUnit();
        List<String> formulars = getFormular();
        int i=0;
       /* for (String tempFormular : formulars) {
            for (String unit : units) {
                String formular="";
                try {
                    formular =new String(tempFormular);
                    formular = formular.replace("{unit}", unit.replace("/", "//"));
                    formular = formular.substring(formular.indexOf("(") + 1, formular.lastIndexOf(")")).replace(" ", "").replace("，", "");  // 去除表达式变量中的空格
                    formular = ("result = " + formular + "; return result");
                    Script script=   GroovyFormular.createScript(formular);
                    i++;
                } catch (Exception e) {
                    logger.warn(" expression is {} error!", formular);
                    continue;
                }
            }
        }*/
        System.out.println("被调用结束"+i);
    }

    //获取unit
    private void setUnit() {
        //获取大区
        List <String>regions=lsh经销商表Repository.getAllRegionEN();
       if(regions!=null)  units.add(regions);
        //获取许可
        for(String region:regions){
        List permits= lsh经销商表Repository.getAllPermitEN(region);
            if(permits!=null)  units.add(permits);
        }
        List <String> pmas= lsh经销商表Repository.getAllPmaEN();
        if(pmas!=null)  units.add(pmas);
        List u=new ArrayList();
        u.addAll( Arrays.asList("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "Q1", "Q2", "Q3", "Q4", "2017"));
        units.add(u );
    }

    //获取Formular
    private List<String> getFormular() {
        List formular = reportConfigRepository.getAllFormularExpression();
        return formular;
    }

    @Autowired
    public void setReportConfigRepository(ReportConfigRepository reportConfigRepository) {
        GroovyFormularConfig.reportConfigRepository = reportConfigRepository;
    }

    @Autowired
    public void setLsh经销商表Repository(Lsh经销商表Repository lsh经销商表Repository) {
        GroovyFormularConfig.lsh经销商表Repository = lsh经销商表Repository;
    }
}
